
#include <iostream>
#include "RessourceManager.h"

using namespace std;
using namespace JDL;

RessourceManager *RessourceManager::_manager = NULL;

RessourceManager::RessourceManager()
{

}

RessourceManager::~RessourceManager()
{
  
}

RessourceManager	*RessourceManager::GetInstance()
{
  if (RessourceManager::_manager == NULL)
    RessourceManager::_manager = new RessourceManager;
  return (_manager);
}

void	RessourceManager::AddImage(const string str)
{
  SDL_Surface	*img;

  img = IMG_Load(str.c_str());
  if (img != NULL)
    _images[str] = img;
}

void	RessourceManager::AddMusic(const string str)
{
  Mix_Music	*mus;

  mus = Mix_LoadMUS(str.c_str());
  if (mus != NULL)
    _musics[str] = mus;
}

void	RessourceManager::AddSound(const string str)
{
  Mix_Chunk	*sound;

  sound = Mix_LoadWAV(str.c_str());
  if (sound != NULL)
    _sounds[str] = sound;
}

SDL_Surface	*RessourceManager::GetImage(const string str)
{
  if (_images.find(str) != _images.end())
    return (_images[str]);
  else
    {
      AddImage(str);
      if (_images.find(str) != _images.end())
	return (_images[str]);
      else
	return (NULL);
    }
}

Mix_Music	*RessourceManager::GetMusic(const string str)
{
  if (_musics.find(str) != _musics.end())
    return (_musics[str]);
  else
    {
      AddMusic(str);
      if (_musics.find(str) != _musics.end())
	return (_musics[str]);
      else
	return (NULL);
    }
}

Mix_Chunk	*RessourceManager::GetSound(const string str)
{
  if (_sounds.find(str) != _sounds.end())
    return (_sounds[str]);
  else
    {
      AddSound(str);
      if (_sounds.find(str) != _sounds.end())
	return (_sounds[str]);
      else
	return (NULL);
    }
}
